package com.shiguiwu.springmybatis.spring.transaction.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * @description: 断言 -> 已经捕获，事务不会回滚. 断言失败，回滚
 * @author: stone
 * @date: Created by 2022/1/9 11:22
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.transaction.demo
 */
@Service
public class ServiceTxA {

    @Autowired
    private ServiceTxB serviceTxB;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @PostConstruct
    public void init() {
        //System.out.println("清空数据库 =====================>");
        //jdbcTemplate.update("truncate table book");
    }

    @Transactional(rollbackFor = Exception.class)
    public void m1() {
        //serviceTxB.m2();
        jdbcTemplate.update("insert into book(book_name) values (?)", "红楼梦");
        if (true) {
            throw new RuntimeException("aa");
        }
        jdbcTemplate.update("insert into book(book_name) values (?)", "红楼梦");

    }
}
